<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Api;
use App\Models\Pointforapi;
use Illuminate\Http\Request;


class PointforController extends ApiController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'pointsfor';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-积分提现接口';
    }

    public function index(){
        global $g_uid;
        $id=$g_uid;
        $this->_response['_active']['_action']      = 'list';

        $pointforModel = new Pointforapi();

        $data = $pointforModel->getData($id);
        if(!empty($data)){
            $this->_response['pointfor'] = $data;
            return response()->json(['code' => 0, 'msg' => '返回成功','data'=>$this->_response]);
        }else{
            return response()->json(['code' => -1, 'msg' => '返回失败']);
        }
    }
    //积分兑换接口
    public function doAdd(\App\Http\Requests\Pointfor $request){
        $data = $request->all();
        $time = time();
        global $g_uid;
        $id=$g_uid;
        $month = date('m',$time);
      //  $id=$data['user_id'];
        $saveData = [
            'points' => $data['points'],
            'money'=>$data['money'],
            'user_id' => $id,
            'withdrawal_address' => $data['withdrawal_address'],
            'created_at' => date('Y-m-d H:i:s', time()),
            'point_month'=>$month,
            'order'=>substr(microtime(),11),
            'iscashback'=>0,
        ];
        $pointforModel = new Pointforapi();

        $setdata=[
          'presentPoints'=>$data['usepoints'],
        ];
        //判断积分是否大于100
        if($data['money']>=100){
            //$pointforModel = new Pointforapi();
            if($pointforModel->saveData($saveData)){
                $updatedpoints =$pointforModel->setpoint($id,$setdata);

                return response()->json(['code' => 0, 'msg' => '提现申请成功']);
            }else{

                return response()->json(['code' => -1, 'msg' => '提现申请失败']);
            }

        }else{
            return response()->json(['code' => -2, 'msg' => '可提现金额小于100不可转账']);
        }

    }

//积分兑换接口

     public function add(\App\Http\Requests\Pointfor $request){
         $data = $request->all();
         $time = time();

         $month = date('m',$time);
         $id=$data['user_id'];
         $saveData = [
             'points' => $data['points'],
             'money'=>$data['money'],
             'user_id' => $id,
             'withdrawal_address' => $data['withdrawal_address'],
             'created_at' => date('Y-m-d H:i:s', time()),
             'point_month'=>$month,
         ];
         $pointforModel = new Pointforapi();

         $setdata=[
             'presentPoints'=>$data['usepoints'],
         ];
         \DB::beginTransaction();
         try{
             //判断积分是否大于100
             if($data['money']>=100){
                 //$pointforModel = new Pointforapi();
                 if($pointforModel->saveData($saveData)){
                    $pointforModel->setpoint($id,$setdata);
                     return response()->json(['code' => 0, 'msg' => '提现申请成功']);
                 }else{

                     return response()->json(['code' => -1, 'msg' => '提现申请失败']);
                 }

             }else{
                 return response()->json(['code' => -2, 'msg' => '可提现金额小于100不可转账']);
             }
             \DB::commit();
         } catch (\Exception $e){
             \DB::rollback();//事务回滚
             return response()->json(['code' => -3, 'msg' => $e->getMessage()]);
             //echo $e->getCode();
         }
     }

}